<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


class PublicAction extends Action
{

    protected $_User;
	protected $_adminCookie;
	protected $_adminSession;

	public function _initialize()
	{
	    $this->_User  = D('User');
		$this->_adminCookie  = cookie('admin');
		$this->_adminSession = $_SESSION['admin'];
	}


	public function login()
	{
		if (!empty($this->adminCookie) && !empty($this->_admin) && 1 == $this->_admin['admin_group'])
		{
			$this->redirect('Index/index');
		} else
		{
			$this->display();
		}
	}


	public function loginAuth()
	{
		$loginPerms = $this->_User->checkLogin();
    	if (!$loginPerms)
    	{
    	    $msg = sprintf(L('login.flood_login_attempts'), get_site_config('max_login_attempts'));
    		$this->error($msg);
    	}

		$clientIP = get_client_ip();
		$whitelist = get_site_config('acp_ip_whitelist');
		if (!empty($whitelist) && C('ACP_CHECK_IP_ENABLE'))
		{
		    $flag = FALSE;
			$list = explode("\n", $whitelist);
			foreach ($list as $ip)
			{
				if (false !== strpos($clientIP, trim($ip)))
				{
					$flag = TRUE;
					break;
				}
			}
			if (!$flag)
			{
			    $msg = sprintf(L('login.invalid_login_ip'), $clientIP);
			    $this->_User->logFailedLogin($loginPerms);
				$this->error($msg);
			}
		}

		if (md5($_POST['verify']) != $_SESSION['verify'])
		{
		    $this->_User->logFailedLogin($loginPerms);
			$this->error(L('login.incorrect_captcha'));
		}

		if (false !== strpos($_POST['account'], '@'))
		{
			$where['email'] = $_POST['account'];
		}
		else
		{
			$where['username'] = $_POST['account'];
		}

		$admin = $this->_User->where($where)->find();
		if (empty($admin))
		{
			$this->_User->logFailedLogin($loginPerms);
			$this->error(L('login.incorrect_account'));
		}
		elseif (1 != $admin['admin_group'])
		{
			$this->_User->logFailedLogin($loginPerms);
			$this->error(L('login.no_acp_perms'));
		}
		elseif (kg_password_encrypt($_POST['password'], $admin['salt']) != $admin['password'])
		{
		    $this->_User->logFailedLogin($loginPerms);
			$this->error(L('login.incorrect_pwd'));
		}
		else
		{
	    	$adminCookie = array(
				'user_id'    => $admin['user_id'],
				'username'   => $admin['username'],
				'user_group' => $admin['user_group'],
			);
			cookie('admin', $adminCookie);
			$_SESSION['admin'] = $admin;
			$this->redirect('Index/index');
		}
	}


	public function license()
	{
	    if (isset($_POST['op']) && 'save' == $_POST['op'])
	    {
	        $Config = D('Config');

	        $name = trim($_POST['software_registration_name']);
		    $license = unserialize(kg_auth_code(trim($_POST['software_license_key']), 'decode', $name));
	    	if (false === strpos($license['domain'], $_SERVER['HTTP_HOST']))
	    	{
	    		$this->error(L('software_license_invalid'));
	    	}
	    	$data = array(
	    	    'software_registration_name' => $_POST['software_registration_name'],
	    	    'software_license_key'       => $_POST['software_license_key'],
	    	    'software_registration_time' => time(),
	    	);
	    	$Config->updateConfigs($data);
	    	$this->assign('jumpUrl', __APP__.'/Admin');
	    	$this->success(L('software_register_succeed'));
	    }

	    $this->display();
	}


	public function logout()
	{
		cookie('admin', null);
		unset($_SESSION['admin']);
		$this->redirect('Public/login');
	}


	public public function captcha()
	{
		$type = isset($_GET['type']) ? $_GET['type'] : 'gif';
        import('ORG.Util.Image');
        Image::buildImageVerify(4, 1, $type);
	}

}
// End of Public Class

/* End of file PublicAction.class.php */
/* Location: ./App/Lib/Action/Admin/PublicAction.class.php */